var $ = require("jquery");
var corpList = JSON.parse(require("../../json/corpJSON.json"));
module.exports = {
    pageDelayTime: 0.5,
    pageDuration: 0.8,
    top: $(window).height(),
    changePage: function(href) {
        var that = this;
        this.pageComponentInit();
        $("#bottomButton").css("height", "0");
        $("#app").css({
            "left": "100%",
            "opacity": "0"
        });
        setTimeout(function () {
            window.location.href = href || "./index.html";
        }, this.pageDelayTime * 1000);
    },
    pageComponentInit: function () {
        this.setTransition($("#app"), 'all', this.pageDelayTime + 's', 'ease');
        this.setTransition($("#bottomButton"), 'all', this.pageDelayTime + 's', 'ease-in-out');
    },
    back: function () {
        this.pageComponentInit();
        $("#bottomButton").css("height", "0");
        $("#app").css({
            "left": "-100%",
            "opacity": "0"
        });
        setTimeout(function () {
            history.go(-1);
        }, (this.pageDelayTime) * 1000);
    },
    setTransition: function (jDom, property, duration, type, delay) {
        var object = {
            property: property || 'all',
            duration: duration || '0.5s',
            type: type || 'linear',
            delay: delay || '0s'
        };
        var transitionStr = object.property + ' ' + object.duration + ' ' + object.type + ' ' + object.delay;
        jDom.css({
            'transition': transitionStr,
            '-moz-transition': transitionStr,
            '-o-transition': transitionStr,
            '-webkit-transition': transitionStr
        });
    },
    getAjaxUrl: function (url) {
        var host = window.location.host;
        var port = '8888';
        var remoteHost = "http://www.sujoe.top:" + port;
        if (host.indexOf('localhost') >= 0) {
            return 'http://localhost:' + port + url;
        } else {
            return remoteHost + url;
        }
    },
    setUserStore: function (phone) {
        localStorage.setItem('phone', phone);
    },
    getUserStore: function () {
        return localStorage.getItem('phone');
    },
    urlGet: function(){
        var aQuery = window.location.href.split("?");//取得Get参数
        var aGET = [];
        if(aQuery.length > 1)
        {
            var aBuf = aQuery[1].split("&");
            for(var i=0, iLoop = aBuf.length; i<iLoop; i++)
            {
                var aTmp = aBuf[i].split("=");//分离key与Value
                aGET[aTmp[0]] = aTmp[1];
            }
        }
        return aGET;
    },
    hackDate: function (str) {
        var reg = new RegExp('-',"g");
        try{
            var date = new Date(str.replace(reg,'/'));
            return date;
        }catch(e){
            var ymd = (str.split(' '))[0];
            var hms = (str.split(' '))[1];
            var ymdArray = ymd.split('-');
            var hmsArray = hms.split(':');
            var year = parseInt(ymdArray[0]);
            var month = parseInt(ymdArray[1])-1;
            var day = parseInt(ymdArray[2]);
            var hour = parseInt(hmsArray[0]);
            var minute = parseInt(hmsArray[1]);
            var second = parseInt(hmsArray[2]);
            return new Date(year,month,day,hour,minute,second);
        }
    },
    getDays: function (date) {
        return parseInt(date.getTime()/1000/60/60/24 - date.getTimezoneOffset()/60/24);
    },
    format: function (date, format) {
        var o = {
            "M+" : date.getMonth()+1, //month
            "d+" : date.getDate(),    //day
            "h+" : date.getHours(),   //hour
            "m+" : date.getMinutes(), //minute
            "s+" : date.getSeconds(), //second
            "q+" : Math.floor((date.getMonth()+3)/3),  //quarter
            "S+" : date.getMilliseconds()
        };
        if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
            (date.getFullYear()+"").substr(4- RegExp.$1.length));
        if(new RegExp("(S+)").test(format)){
            format=format.replace(RegExp.$1,
                RegExp.$1.length==1? o["S+"] :
                    ("000"+ o["S+"]).substr((""+ o["S+"]).length));
        }
        for(var k in o)if(new RegExp("("+ k +")").test(format))
            format = format.replace(RegExp.$1,
                RegExp.$1.length==1? o[k] :
                    ("00"+ o[k]).substr((""+ o[k]).length));

        return format;
    },
    getCorpItem: function (com) {
        var chooseItem = corpList.filter(function (each) {
            return each.com == com;
        });
        return chooseItem[0];
    },
    showToast: function (args) {
        //向页面输出东西;
        var time = new Date().getTime();
        var html =
        '<div class="dialog-toastContainer" index="'+time+'">' +
            args.content +
        '</div>';
        $('body').append(html);
        var jdom = $('.dialog-toastContainer[index="'+time+'"]');
        //对容器的css进行计算并重置
        var a = $('.dialog-toastContainer[index="'+time+'"]')[0];
        var leftoffset = (document.body.clientWidth/2-a.clientWidth/2)+"px";
        var topoffset = (document.body.scrollTop+$(window).height()/2-a.clientHeight/2)+"px";
        $('.dialog-toastContainer').css({'left':leftoffset,'top':topoffset});
        return jdom;
    },
    showLoading: function () {
        var makeItRun = function (jDom) {
            var index = 0;
            setInterval(function () {
                index++;
                jDom.css("background-position-y", index%3*(-117)+'rem');
            },800);
        };
        //向页面输出东西;
        var time = new Date().getTime();
        var html =
        '<div class="dialog-loadingContainer" index="'+time+'">' +
            '<div class="contain"></div>' +
        '</div>';
        $('body').append(html);
        var jdom = $('.dialog-loadingContainer[index="'+time+'"]');
        //对容器的css进行计算并重置
        var a = jdom[0];
        var leftoffset = (document.body.clientWidth/2-a.clientWidth/2)+"px";
        var topoffset = (document.body.scrollTop+$(window).height()/2-a.clientHeight/2)+"px";
        $('.dialog-loadingContainer').css({'left':leftoffset,'top':topoffset});
        makeItRun(jdom.find(".contain"));
        return jdom;
    }
};